package com.op.teachingliteapp.fragment.viewer;

import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Gallery;
import com.op.teachingliteapp.R;
import com.op.teachingliteapp.sender.ui.view.BrushesView;
import com.op.teachingliteapp.sender.ui.view.PdfViewPager;
import com.op.teachingliteapp.sender.ui.view.thumbnailgallery.ThunbmailAdapter;
import com.op.teachingliteapp.util.DensityUtils;
import com.op.teachingliteapp.util.ScreenSendUtils;
import com.op.teachingliteapp.util.SharedPreferenceUtil;


/**
 * Created by Genji on 2015/7/5.
 */
public class ViewPagerFragment extends Fragment {
    private PdfViewPager pdfViewPager;
    private View root, penSetting;
    private BrushesView brushesView;
    private String TAG = this.getClass().getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.root = inflater.inflate(R.layout.fragment_viewpager, null);
        this.penSetting = inflater.inflate(R.layout.set_pen, null);
        return this.root;
    }

    @Override
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String url = getArguments().getString("url");

        pdfViewPager = (PdfViewPager) view.findViewById(R.id.viewPager);
        Button button = (Button) view.findViewById(R.id.pen);
        Button showThunbnailBtn = (Button) view.findViewById(R.id.showThunbnail);


        brushesView = new BrushesView(getActivity(),this.penSetting, DensityUtils.dip2px(getActivity(), 600), DensityUtils.dip2px(getActivity(), 70), button);
        PDFAdapter pdfAdapter = new PDFAdapter(getActivity(), url);
        pdfViewPager.setAdapter(pdfAdapter);
        SharedPreferences preferences = SharedPreferenceUtil.getInstance(getActivity());

        pdfViewPager.setSelected(true);
        brushesView.associate(pdfViewPager);
        pdfAdapter.setPdfViewPager(pdfViewPager);
        final Gallery gallery = (Gallery) view.findViewById(R.id.thumbnailGallery);
        showThunbnailBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (gallery.getVisibility() == view.GONE) {
                    gallery.setVisibility(View.VISIBLE);
                } else {
                    gallery.setVisibility(View.GONE);
                }

            }
        });
        gallery.setAdapter(new ThunbmailAdapter(getActivity(), url));
        gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                pdfViewPager.setCurrentItem(position, true);
            }
        });


    }

    @Override
    public void onResume() {
        super.onResume();
        ScreenSendUtils.sendWithDely(this.pdfViewPager);

    }

    @Override
    public void onDestroy() {
        brushesView.dismiss();
        System.gc();
        Log.i(TAG, "onDestroy");

        super.onDestroy();
    }
}
